home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * Copyright (C) 1992 Silicon Graphics, Inc.
- *
- _______________________________________________________________________
- ______________ S I L I C O N G R A P H I C S I N C . ____________
- |
- | $Revision: 1.0000 $
- |
- | File: walk.c++
- | Purpose: creates a walk viewer, when the user comes out of viewing
- | mode and hits the d key, information about the camera
- | location gets spit out, this is useful for creating
- | animation files to be played with the player program
- |
- | Author(s) : Kevin Goldsmith
- |
- ______________ S I L I C O N G R A P H I C S I N C . ____________
- _______________________________________________________________________
- */
-
-
-
-
- #include <assert.h>
- #include <getopt.h>
-
- #include <Inventor/Xt/SoXt.h>
- #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
- #include <Inventor/nodes/SoNode.h>
- #include <Inventor/nodes/SoSeparator.h>
- #include <Inventor/nodes/SoPerspectiveCamera.h>
- #include <Inventor/SoDB.h>
- #include <Inventor/nodes/SoCallback.h>
- #include <Inventor/actions/SoHandleEventAction.h>
- #include <Inventor/events/SoKeyboardEvent.h>
-
- SoCamera *camera = NULL;
-
- static char *filename = NULL;
- static char *envfile = NULL;
-
- void
- CreateCamera(SoGroup *root)
- {
- SoPerspectiveCamera *pc = new SoPerspectiveCamera;
- pc->ref();
- camera = pc;
- root->insertChild(pc, 0);
- }
-
- //
- // Read a file given a filename and return a separator containing all
- // of the stuff in the file.
- // --- stolen mostly from Gavin Bell
- //
- SoSeparator *
- readFile(const char *filename)
- {
- SoInput in;
- if (filename != NULL)
- {
- if (in.openFile(filename) == FALSE)
- {
- fprintf(stderr, "Could not open file %s\n", filename);
- return NULL;
- }
- }
-
- SoSeparator *graph = new SoSeparator;
- graph->ref();
-
- //
- // Keep on reading until there are no more nodes to read
- //
- SoNode *root;
- do
- {
- int read_ok = SoDB::read(&in, root);
-
- if (!read_ok)
- {
- fprintf(stderr, "Error reading file\n");
- graph->unref();
- return NULL;
- }
- else if (root != NULL) graph->addChild(root);
-
- } while (root != NULL);
- in.closeFile();
-
- //
- // Try to avoid creating extra separators; if this scene graph
- // already has exactly one separator at the top, use it. This
- // will avoid an explosion of separators at the top of scenes that
- // would otherwise occur if we automatically created a new
- // separator every time a scene graph was read.
- //
- if (graph->getNumChildren() == 1 &&
- graph->getChild(0)->isOfType(SoSeparator::getClassTypeId()))
- {
- SoSeparator *result = (SoSeparator *)graph->getChild(0);
- result->ref(); // Note the order here!
- graph->unref();
-
- result->unrefNoDelete();
- return result;
- }
-
- graph->unrefNoDelete();
- return graph;
-
- }
-
- // did the user press the key we want?
- static void checkKey(void *, SoAction *action)
- {
- if (action->getTypeId().
- isDerivedFrom(SoHandleEventAction::getClassTypeId())) {
- const SoEvent *e = ((SoHandleEventAction *)action)->getEvent();
-
- // did the user hit the d?
- if (SO_KEY_PRESS_EVENT(e, D)) {
- assert(camera != NULL);
- SbVec3f pos;
- pos = camera->position.getValue();
-
- SbVec3f axis;
- float angle;
-
- camera->orientation.getValue(axis, angle);
- printf("%f %f %f %f %f %f %f\n", pos[0],
- pos[1], pos[2], axis[0], axis[1], axis[2], angle);
- }
- }
- }
-
-
- // get user flags
- static void
- parseCommandLine(int argc, char **argv)
- {
- int err = 0; // Flag: error in options?
- int c;
-
- while ((c = getopt(argc, argv, "e:")) != -1)
- {
- switch(c)
- {
- case 'e': envfile = optarg; break;
- default:
- err = 1;
- break;
- }
- }
- if (optind+1 != argc) err = 1;
-
- filename = argv[optind];
-
- if (err)
- {
- fprintf(stderr, "Usage: %s "
- "[-e envfile] filename\n", argv[0]);
- fprintf(stderr, "-e : Reads given environment file\n");
- exit(99);
- }
- }
-
-
-
- main( int argc, char **argv )
- {
- parseCommandLine(argc, argv);
-
- Widget top = SoXt::init(argv[0]);
- if (top == NULL)
- {
- fprintf(stderr, "%s: Couldn't open top-level widget\n", argv[0]);
- exit(1);
- }
-
- // create the viewer
- SoXtWalkViewer *ra = new SoXtWalkViewer(top);
-
- // create the root of the subgraph
- SoSeparator *root = new SoSeparator;
- root->ref();
-
- // create a callback to see if we get the key
- SoCallback *callback = new SoCallback;
- callback->setCallback(checkKey);
- root->addChild(callback);
-
-
- // the user wants to start at an envfile
- if (envfile != NULL)
- {
- SoSeparator *env = readFile(envfile);
- if (env == NULL) exit(1);
- env->ref();
- for (int i = 0; i < env->getNumChildren(); i++)
- {
- root->addChild(env->getChild(i));
- if (env->getChild(i)->isOfType(SoCamera::getClassTypeId()))
- camera = (SoCamera *)env->getChild(i);
- }
- env->unref();
- } else
- CreateCamera(root); // otherwise we create our own camera
-
- // read in the inventor file
- SoSeparator *stuff = readFile(filename);
- if (stuff == NULL) exit(1); // it was empty!!
- root->addChild(stuff); // or it was not
-
- // set some stuff on the render area
- ra->setSceneGraph(root);
- ra->setHeadlight(TRUE);
- ra->setDecoration(TRUE);
-
- //show the viewer
- ra->show();
-
- // show the window
- SoXt::show(top);
-
- // let Inventor take care of the rest
- SoXt::mainLoop();
- }
-